package com.neu.fenli2023623.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.fenli2023623.dto.HealthEncyclopediaDto;
import com.neu.fenli2023623.entity.HealthEncyclopedia;
import com.neu.fenli2023623.service.HealthEncyclopediaService;
import com.neu.fenli2023623.util.ResultVo;
import com.neu.fenli2023623.vo.DoctorVo;
import com.neu.fenli2023623.vo.HealthEncyclopediaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/health")
public class HealthEncyclopediaController {
    @Autowired
    private HealthEncyclopediaService healthEncyclopediaService;

    @RequestMapping("quaryallhealth")
    public ResultVo quaryall(@RequestBody HealthEncyclopediaDto healthEncyclopediaDto){
        PageHelper.startPage(healthEncyclopediaDto.getCurrentPage(),healthEncyclopediaDto.getPageSize(),true);
        List<HealthEncyclopediaVo> healthEncyclopediaList = healthEncyclopediaService.quaryall();
        PageInfo<HealthEncyclopediaVo> pageInfo = new PageInfo<>(healthEncyclopediaList);
        return ResultVo.ok().data("healthlist1",healthEncyclopediaList).data("total",pageInfo.getTotal());
    }//

    @RequestMapping("addHealth")
    public ResultVo addHealth(@RequestBody HealthEncyclopediaDto record){
        int i = healthEncyclopediaService.addHealthEncyclopedia(record);
        if(i>0){
            return ResultVo.ok("添加成功");
        }else {
            return ResultVo.error("添加失败");
        }
    }

    @DeleteMapping("{id}")
    public ResultVo deleteHealth(@PathVariable("id") Long id){
        int i = healthEncyclopediaService.deleteHealthEncyclopediaByPrimaryKey(id);
        if (i > 0) {
            return ResultVo.ok("删除成功");
        } else {
            return ResultVo.error("删除失败");
        }
    }//删除

    @PostMapping("{name}")
    public ResultVo selectByName(@PathVariable("name") String name){
        List<HealthEncyclopedia> healthEncyclopedias = healthEncyclopediaService.selectByName(name);
        if(healthEncyclopedias!=null){
            return ResultVo.ok().data("healthlist",healthEncyclopedias);
        }else {
            return ResultVo.error("查询失败");
        }
    }//查询

    @GetMapping("{id}")
    public ResultVo selectById(@PathVariable("id")Long id){
        HealthEncyclopedia healthEncyclopedia = healthEncyclopediaService.selectHealthEncyclopediaByPrimaryKey(id);
        return ResultVo.ok().data("health",healthEncyclopedia);
    }

    @RequestMapping("updateHealth")
    public ResultVo update(@RequestBody HealthEncyclopediaDto record){
        int i = healthEncyclopediaService.updateHealthEncyclopediaByPrimaryKey(record);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }//更新

}
